package com.atguigu.mall.coupon.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.atguigu.common.to.MemberPrice;
import com.atguigu.common.to.SkuReductionTO;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.mall.coupon.dao.SkuFullReductionDao;
import com.atguigu.mall.coupon.entity.MemberPriceEntity;
import com.atguigu.mall.coupon.entity.SkuFullReductionEntity;
import com.atguigu.mall.coupon.entity.SkuLadderEntity;
import com.atguigu.mall.coupon.service.MemberPriceService;
import com.atguigu.mall.coupon.service.SkuFullReductionService;
import com.atguigu.mall.coupon.service.SkuLadderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商品满减信息
 */
@Service("skuFullReductionService")
@Transactional
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Resource
    private SkuLadderService skuLadderService;
    @Resource
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveFullInfo(SkuReductionTO skuReductionTO) {
        Long skuId = skuReductionTO.getSkuId();
        /**
         * sms_sku_ladder  sku 打折表
         * sms_sku_full_reductioe 会员价格表n 满减表
         * sms_member_pric 会员价格表
         * sms_spu_bounds 积分表
         */
        //sms_sku_ladder  sku 打折表
        SkuLadderEntity skuLadder = new SkuLadderEntity();
        BeanUtil.copyProperties(skuReductionTO,skuLadder);
//        skuLadder.setSkuId(skuId);
//        skuLadder.setFullCount(skuReductionTO.getFullCount());
//        skuLadder.setDiscount(skuReductionTO.getDiscount());
//        skuLadder.setAddOther(skuReductionTO.getCountStatus());
        /**
         * TODO 计算折后价
         */
        skuLadderService.save(skuLadder);
//        sms_sku_full_reductioe
        SkuFullReductionEntity skuFullReduction = new SkuFullReductionEntity();
        BeanUtil.copyProperties(skuReductionTO,skuFullReduction);
        BigDecimal fullPrice = skuFullReduction.getFullPrice();
        /**
         * 满减大于0 才录入数据
         */
        if (BigDecimal.ZERO.compareTo(fullPrice)>0){
            this.save(skuFullReduction);
        }


//        会员价格表
        List<MemberPrice> memberPrice = skuReductionTO.getMemberPrice();
        List<MemberPriceEntity> memberPriceEntities = memberPrice.stream()
                .filter(item-> item.getPrice().compareTo(BigDecimal.ZERO)>0)
                .map(member -> {
            MemberPriceEntity priceEntity = new MemberPriceEntity();
            priceEntity.setSkuId(skuId);
//            会员等级id
            priceEntity.setMemberLevelId(member.getId());
//            会员等级名称
            priceEntity.setMemberLevelName(member.getName());
//            会员价格
            priceEntity.setMemberPrice(member.getPrice());
            return priceEntity;
        }).collect(Collectors.toList());
        memberPriceService.saveBatch(memberPriceEntities);
    }

}